Here we imported all the packages need fot the analysis

Next we set the current datetime to today's and the time duration to 1year

Next, we fetch the financial data of several companies we are intrested in

It shows the first 5 columns of the financial record of the choosenn stock, it helps us to analyze how the data is categorised.

We do the same for all stocks we choose

info() gives us more insights into the dataset, its dimensions, its class, number of entries etc

Here our main focus is on "Adjusted Close" section of the data, it means the cash value of the last transacted price before the market closes. The adjusted closing price is attributed to anything that would affect the stock price after the market closes for the day.

As we can see it accurately predicted the closing price of the time series. It can't be done by simple returns as if we want to model returns using the normal distribution! Simple Returns: The product of normally distributed variables is not normally distributed. Log Returns: The sum of normally distributed variables folllow a normal distribution.

As we see by simple returns the prediction is close to the actually closing price but not accurately close, also its very non-intutive to follow through

As we can see it's mostly centered around 0, looks a bit normally distributed. But there is quite a catch here is normality a good assumption for financial data? The assumption that prices or more accurately log returns are normally distributed.

As we can see the deviation is huge, as in normal distribution about 99.75% of the data is within 3 standard deviations, which is just not the case here.

Q-Q or Quantile-Quantile Plots It plots two sets of quantiles against one another i.e. theoritical quantiles against actual quantiles of the variable.

As we can see from here normally treating financial data as normally distributed is not a bad assumption for the most part, except for the tails. Which we can see from the plot as well, at the tails and heads there seems a deviation from normality.

We do the same for other assets as well, it would help us to annalyse how it is varrying wrt days

This code calculates the moving averages (MA) for a given set of days (10, 20, and 30) for the 'Adj Close' price data

For a specified company's stock market data, the code defines a function called plot_graphic, which creates a graph illustrating the adjusted closing prices and three moving averages (MA: 10, MA: 20, and MA: 30). The function takes two parameters: company, which represents the data for the company, and company_string, which represents the company's name. An effective way to visualize adjusted closing prices and moving averages of a specific company's stock market data is to use the plot_graphic function. Over time, it makes it easier to analyze and compare price trends. The more days we incorporate the more smoothing we get, which can be seen in the graph below.

Data sets for different companies are iterated through in the data_list. A plot_graphic function is invoked for each data set, with the corresponding company data and name provided as input. As a result, distinct graphs are created for each company, which allow visualization of their adjusted closing prices and moving averages. As u can see the red line gives more smoothening over blue line, which gives more smoothening over green line.

This code snippet calculates the daily returns for each company within the data_list and visualizes the distribution of these returns using a histogram. On observing we see it follows almost a normal distribution, we say “almost” as the head and tail parts of the histogram (upon which we did smoothening to arrive at this conclusion) don’t follow Gaussian distribution very strictly (why? And how can we say this? I have explained this in my talk of normality section). And the smoothening has been achieved due to the usage of moving average, as histogram itself is a discrete distribution and in order to achieve a continuous distribution moving average provides a good means for visualization.

Several operations are carried out using the pandas library in the provided code: First, it imports the pandas library, denoted by import pandas as pd. In this way, pandas can be used to manipulate and analyze data. The next step is to create a list called daily_returns_list by using a list comprehension. Each dictionary in data_list is iterated through to retrieve the values associated with the key 'Daily Returns'. Stock_returns is generated by concatenating the pandas Series objects from daily_returns_list along the columns axis using the pd.concat() function. A combined view of the daily returns data for all of the companies is provided by this DataFrame, in which each column represents the daily returns of a different company. Stock_returns.columns = company_list ensures that the column names in stock_returns align with the names of the companies. Lastly, stock_returns.head() is used to display the first few rows of the resulting DataFrame. A list of dictionaries containing daily returns data for different companies is processed by this code. It allows comparison and analysis of the daily returns across companies by concatenating them into a single DataFrame. Stock_returns, the resulting DataFrame, consolidates the daily returns by company, with each column denoting a specific company.

In this code snippet, sns.pairplot(stock_returns.dropna()) uses the Seaborn library to create a scatter plot matrix based on the stock_returns DataFrame. A breakdown of the code can be found here: The sns.pairplot() function produces a grid of scatter plots that display pairwise comparisons of variables within a dataset. The stock_returns DataFrame will be plotted with pairplot(). The stock_returns.dropna() operation ensures that the scatter plot matrix contains complete data. The dropna() function removes any rows in stock_returns that contain missing values, denoted as NaN. Seaborn's pairplot() function is used to create a scatter plot matrix. In this matrix, you can see how variables within the stock_returns DataFrame are related pairwise. Dropna() ensures that data for scatter plots is complete. The resulting plot can provide insight into the correlations and patterns between the daily returns of different companies.

A heatmap plot is generated to visualize the correlations between stock_returns and the correlation matrix created in the provided code snippet.

randomPortfolio() is responsible for generating a random portfolio of shares. The randomPortfolio() function generates a random portfolio of shares by drawing random values from a standard normal distribution, exponentiating them to ensure positive values, and then normalizing them to represent proportions of the total portfolio value. By calling this function, you can obtain a random allocation of shares for a portfolio.

The provided code includes two functions: IncomePortfolio(Rand) and RiskPortfolio(Rand). These functions are designed to perform calculations related to income and risk for a portfolio. the IncomePortfolio(Rand) function calculates the expected income of a portfolio based on the mean income values and the allocation of assets. The RiskPortfolio(Rand) function, on the other hand, calculates the risk of a portfolio based on the covariance matrix of returns and the allocation of assets. Together, these functions provide essential metrics for evaluating the income and risk characteristics of a portfolio

First, the variable "combinations" is initialized to 10000. This variable determines the number of portfolio combinations that will be generated and evaluated Next, three arrays named "risk," "income," and "portfolio" are created and initialized with zeros. These arrays will store the risk, income, and portfolio data for each combination. Within the loop, the function "randomPortfolio()" is called to generate a random portfolio. It generates a set of shares along with their corresponding weights. The randomly generated portfolio is then assigned to the ith row of the "portfolio" array. Each row in the "portfolio" array represents a different combination of shares. The "RiskPortfolio()" function is invoked, passing the current portfolio as an argument. This function calculates the risk associated with the given portfolio.The calculated risk value is assigned to the ith element of the "risk" array. The "IncomePortfolio()" function is called with the current portfolio as an argument. This function computes the income or expected return of the portfolio.The calculated income value is then assigned to the ith element of the "income" array.

The ratio is the average return earned in excess of the risk-free rate per unit of volatility or total risk. Volatility is a measure of the price fluctuations of an asset or portfolio. The risk-free rate of return is the return on an investment with zero risk, meaning it’s the return investors could expect for taking no risk. The optimal risky portfolio is the one with the highest Sharpe ratio.

The provided code snippet utilizes the matplotlib library to create a scatter plot visualizing the relationship between risk and income, which were calculated in the previous code. It generates a scatter plot where the x-axis represents the risk and the y-axis represents the income for multiple portfolios. It further highlights the portfolio with the maximum Sharpe ratio by adding a red dot at its corresponding risk and income values and including a legend to identify it. The scatter plot provides a visual representation of the risk and income relationship for the portfolios.

It identifies the portfolio with the highest Sharpe ratio and then displays the allocation or weight assigned to each company in that portfolio. It allows us to see how the assets or companies are distributed within the best-performing portfolio.

It calculates the average daily returns (mu) and the standard deviation of daily returns (sigma) for a stock based on the available data. These values are commonly used in financial analysis and risk assessment to understand the historical performance and volatility of a stock.

The provided code snippet introduces a function named monte_carlo that employs the Monte Carlo method to simulate the future price of a stock. The monte_carlo function uses the Monte Carlo method to generate simulated stock prices for a specified number of days. It considers the initial stock price, average daily return, and standard deviation of daily returns. The function incorporates random shocks and drift components to calculate the simulated prices for each day. For the generation of random paths, I have used arithmetic Brownian motion, instead of this one can use geometric Brownian motion too. The more the variance the more the spread is, and the less the steepness is.In context to Monte Carlo Simulation, the random paths generated would be less differentiating if the variance is less and it would be more if the variance is more resulting in a flatter curve.

The code snippet in question carries out a Monte Carlo analysis for Twitter's stock, which is denoted by the ticker symbol 'TWTR'. It commences by assigning the initial stock price of Twitter at $35.65, as indicated by the line "start_price = 35.65". The next line, "sim = np.zeros(1000)", establishes an array named 'sim' composed of 1000 zeros. These zeros are placeholders for the final predicted stock prices following the Monte Carlo simulations. This code performs a Monte Carlo analysis of Twitter's stock by predicting its future prices through 1000 simulations. The final prices from these simulations are stored in the 'sim' array and plotted. By doing so, the code offers potential insights into the future price range of Twitter's stock, as determined by the Monte Carlo simulations.

The provided code constructs a histogram to illustrate the distribution of the Twitter stock's simulated prices derived from the Monte Carlo simulations. The visualization includes text annotations that outline the mean, standard deviation, and initial price of the simulated prices. This visual representation provides valuable insights into the potential range and traits of Twitter's future stock prices, as per the Monte Carlo simulations.

We did the same analysis for other stocks as well, which we discussed in detail for Twitter.

We can also extend this discussion to include risk factor along with it to incorporate more diversity